using System;

using System.Linq;
using BLToolkit.Data;
using Rsdn.SmartApp;

namespace Rsdn.Janus
{
    internal static class XmlBuilder
    {
        public static XmlMessage BuildMessage(
            IServiceProvider provider,
            IJanusDatabaseManager dbMgr,
            int mid)
        {
			using (var db = dbMgr.CreateDBContext())
            {
                var msg =
                    db
                        .Message(
                            mid,
                            m =>
                                new
                                {
                                    m.UserID,
                                    m.UserClass,
                                    UserNick = m.UserNick.ToUserDisplayName(m.UserClass),
                                    m.Date,
                                    m.IsRead,
                                    m.IsMarked,
                                    m.ArticleId,
                                    m.Name,
                                    m.Subject,
                                    m.Message,
                                    m.User.Origin,
                                    Rating = m.Rating(),
                                    Smiles = m.SmileCount(),
                                    Agrees = m.AgreeCount(),
                                    Disagrees = m.DisagreeCount()
                                });
                IJanusFormatter formatter = provider.GetFormatter();

                XmlMessage xmlMessage = new XmlMessage();
                xmlMessage.FormattingOptions.ShowHeader = Config.Instance.ForumDisplayConfig.Envelope.ShowHeader;
                xmlMessage.FormattingOptions.ShowRateFrame = Config.Instance.ForumDisplayConfig.Envelope.ShowRateFrame;

                xmlMessage.ID = mid;

                xmlMessage.Author.ID = msg.UserID;
                xmlMessage.Author.UserClass = (int)msg.UserClass;
                xmlMessage.Author.DisplayName = msg.UserNick;

                xmlMessage.Date.Value = msg.Date.ToString(Config.Instance.ForumDisplayConfig.DateFormat);
                xmlMessage.Date.IsOutdate = DateTime.Now.AddDays(-Config.Instance.ForumDisplayConfig.DaysToOutdate) > msg.Date
                                            && Config.Instance.ForumDisplayConfig.DaysToOutdate != 0;
                xmlMessage.Date.DayOfWeek = Convert.ToInt32(msg.Date.DayOfWeek);

                xmlMessage.IsUnread = !msg.IsRead;
                xmlMessage.IsMarked = msg.IsMarked;
                xmlMessage.ArticleID = msg.ArticleId.GetValueOrDefault();
                xmlMessage.Name = msg.Name;
                xmlMessage.Subject = msg.Subject;
                xmlMessage.Content = formatter.Format(msg.Message, true);
                xmlMessage.Origin = formatter.Format(msg.Origin, true);

                IFormatMessageHelperService formatMessageService =
                    provider.GetRequiredService<IFormatMessageHelperService>();

                xmlMessage.Rate.Summary = formatMessageService.FormatRates(msg.Rating, msg.Smiles, msg.Agrees, msg.Disagrees);

                if (Config.Instance.ForumDisplayConfig.Envelope.ShowRateFrame)
                {
                    var rateList =
                        db
                            .Rates(r => r.MessageID == mid)
                            .OrderByDescending(r => r.Date)
                            .Select(
                                r =>
                                    new
                                        {
                                            r.RateType,
                                            r.Multiplier,
                                            r.Message.ServerForum.InTop,
                                            r.UserID,
                                            r.User.UserClass,
                                            DisplayName = r.User.DisplayName()
                                        });
                    foreach (var rate in rateList)
                    {
                        XmlMessage.MessageRateItem rateItem = new XmlMessage.MessageRateItem();
                        string rateString = string.Empty;
                        int intType = (int)rate.RateType;
                        if (intType > 0)
                        {
                            rateString = (rate.Multiplier*intType).ToString();
                        }

                        rateItem.Value = rateString;
                        rateItem.ForumInTop = rate.InTop;
                        rateItem.Type = (int)rate.RateType;

                        rateItem.Author.ID = rate.UserID;
                        rateItem.Author.UserClass = (int)rate.UserClass;
                        rateItem.Author.DisplayName = rate.DisplayName;

                        xmlMessage.Rate.List.Add(rateItem);
                    }
                }
                return xmlMessage;
            }
        }
    }
}